package com.example.demo.DBServer.thisDatabase.service.impl;

import com.example.demo.DBServer.thisDatabase.dao.ExecuteInformationDao;
import com.example.demo.DBServer.thisDatabase.service.ExecuteConditionService;
import com.example.demo.DBServer.thisDatabase.service.ExecuteInformationService;
import com.example.demo.DBServer.thisDatabase.service.RelatedExecuteInformationService;
import com.example.demo.entity.ExecuteConditionEntity;
import com.example.demo.entity.ExecuteInformationEntity;
import com.example.demo.entity.RelatedExecuteInformationEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ExecuteInformationServiceImpl implements ExecuteInformationService {

    @Autowired
    private ExecuteInformationDao executeInformationDao;

    @Autowired
    private ExecuteConditionService executeConditionService;

    @Autowired
    private RelatedExecuteInformationService relatedExecuteInformationService;

    @Override
    public void saveExecuteInformation(ExecuteInformationEntity executeInformation) {
        executeInformationDao.saveExecuteInformation(executeInformation);
        ExecuteConditionEntity executeCondition = executeInformation.getExecuteCondition();
        if(executeCondition != null) {
            executeCondition.setExecuteInformationId(executeInformation.getId());
            executeConditionService.saveExecuteCondition(executeCondition);
        }
        List<RelatedExecuteInformationEntity> relatedExecuteInformation = executeInformation.getRelatedExecuteInformation();
        if(relatedExecuteInformation!=null && relatedExecuteInformation.size()>0){
            relatedExecuteInformation.forEach(info -> info.setExecuteInformationId(executeInformation.getId()));
            relatedExecuteInformationService.batchSaveRelatedExecuteInformation(relatedExecuteInformation);
        }
    }

    @Override
    public List<ExecuteInformationEntity> findAllByTableName(String tableName) {
        return null;
    }

    @Override
    public ExecuteInformationEntity findOneById(Long id) {
        ExecuteInformationEntity executeInformation = executeInformationDao.findOneById(id);
        if(executeInformation != null) {
            executeInformation.setExecuteCondition(executeConditionService.findAllByExecuteInformationId(id));
            executeInformation.setRelatedExecuteInformation(relatedExecuteInformationService.findAllByExecuteInformationId(id));
        }
        return executeInformation;
    }
}
